package com.xk.school_device.controller;

import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xk.school_device.common.Result;
import com.xk.school_device.entity.rep.RePasswordReq;
import com.xk.school_device.entity.Users;
import com.xk.school_device.service.IUsersService;
import com.xk.school_device.utlis.PasswordUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;


/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author xk
 * @since 2025-04-16
 */
@RestController
@RequestMapping("/api/user")
public class UsersController {
    @Autowired
    private IUsersService usersService;

    /**
     * 登录
     * @param user
     * @return
     */
    @PostMapping("/login")
    public Result login(@RequestBody Users user){

        return usersService.login(user);
    }
//    @PostMapping("/register")
//    public Result register(@RequestBody Users user){
//
//        return usersService.register(user);
//    }

    /**
     * 注销
     * @param user
     * @return
     */
    @PostMapping("/logout")
    public Result logout(@RequestBody Users user){
        return usersService.logout(user);
    }
    /**
     * 测试权限
     */
    @PostMapping("/permission")
    public Result permission(){
        if (StpUtil.hasPermission("role_sys_admin")){
            return Result.success("测试权限成功");
        }
        return Result.success("权限不够");
    }

    /**
     * 修改用户信息
     */
    @PostMapping("/updatePhoneUsername")
    public Result update(@RequestBody Users user){
        return usersService.updateUserInfoById(user);
    }
/**
 * 更换头像
 */

    @PostMapping("/updateAvatar")
    public Result updateAvatar(@RequestParam("userId") String id, @RequestParam("file") MultipartFile file){
        return usersService.updateAvatar(id,file);
    }
    /**
     * 重置密码
     */
    @PostMapping("/restPassword")
    public Result restPassword(@RequestBody RePasswordReq rePasswordReq) {
        usersService.restPassword(rePasswordReq.getId(), rePasswordReq.getRePassword());
        return Result.success("重置成功");
    }

    @PostMapping("/getUser")
    public Result getUser(@RequestBody Users user){
        return usersService.getUser(user);
    }

    /**
     * 获取用户列表
     */
    @GetMapping("/list")
    public Result getUserList(@RequestParam(required = false) String username,
                            @RequestParam(required = false) String phone,
                            @RequestParam(defaultValue = "1") Integer current,
                            @RequestParam(defaultValue = "10") Integer size) {
        LambdaQueryWrapper<Users> queryWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.hasText(username)) {
            queryWrapper.like(Users::getUsername, username);
        }
        if (StringUtils.hasText(phone)) {
            queryWrapper.like(Users::getPhone, phone);
        }
        Page<Users> page = new Page<>(current, size);
        Page<Users> userPage = usersService.page(page, queryWrapper);
        return Result.success(userPage);
    }

    /**
     * 添加用户
     */
    @PostMapping("/add")
    public Result addUser(@RequestBody Users user) {
        // 设置默认密码
        user.setPassword(PasswordUtil.encrypt("123456"));
        boolean success = usersService.save(user);
        return success ? Result.success("添加成功") : Result.failure("添加失败");
    }

    /**
     * 编辑用户
     */
    @PostMapping("/edit")
    public Result editUser(@RequestBody Users user) {
        boolean success = usersService.updateById(user);
        return success ? Result.success("编辑成功") : Result.failure("编辑失败");
    }

    /**
     * 删除用户
     */
    @DeleteMapping("/{id}")
    public Result deleteUser(@PathVariable String id) {
        boolean success = usersService.removeById(id);
        return success ? Result.success("删除成功") : Result.failure("删除失败");
    }
}
